﻿var NewslistHeadline = function(rHd){
    var __hd = (typeof(rHd)=="string")?rHd:"newslistHeadline";
    var __data = null;
    var __box = null;
    var __requestUrl = null;
    var __DATA_ITEM = function(){
        this.id = "";
        this.imgSrc = "";
        this.url = "";
        this.title = "";
        this.target = "";
    };
    var __timer = null;
    var __step = 5000;//time:ms
    var __index = 0;
    var __ajax = null;
    var __key = jskitUtil.guid();
    var __img = null;
    var __bimg = null;
    var __numbar = null;
    var __maxCount = 5;
    
    var __buildContent = function(){
        var _data = __data[__index];
        var _str = new Array();
        _str.push('<div class="smarted_convas">');
        _str.push('<div class="smartad_pic_bar">');
        _str.push('<a href="Biz/News/Details.aspx?id='+_data.NewsKey+'"><img id="img_'+__key+'" src="'+_data.CoverPath+'" alt="'+_data.Title+'" /></a>');
        //_str.push('<img id="bimg_'+__key+'" src="'+_data.CoverPath+'" alt="" />');
        _str.push('</div>');
        _str.push('<div class="smartad_num_bar" id="num_'+__key+'">');
        for(var i=0;i<__data.length;i++){
            if(i==__index){
                _str.push('<div class="smartad_num_i_c" onclick="'+__hd+'.change('+i+');">'+(i+1)+'</a></div>');        
            }else{
                _str.push('<div class="smartad_num_i" onclick="'+__hd+'.change('+i+');">'+(i+1)+'</div>');        
            }
        }
        _str.push('</div>');
        _str.push('</div>');
        return _str.join('');
    };
    
    var __loadData = function(){
        __stop();
        if(__maxCount>0){
            if(__requestUrl.indexOf("?")!=-1){
                __requestUrl += "&top="+__maxCount;
            }else{
                __requestUrl += "?top="+__maxCount;
            }
            __requestUrl += "&rm="+jskitUtil.guid();
        }
        __ajax = new JskitXmlHttpAction(__requestUrl,__hd+".__loadDataCallback","text");    
    };
    this.__loadDataCallback = function(json){
        __ajax = null;
        try{
            eval("__data = "+json+";");
            if(__data!=null && __data.length>0){
                __box.innerHTML = __buildContent();
                __img = $("#img_"+__key);
                __bimg = $("#bimg_"+__key);
                __numbar = $("#num_"+__key);
                __timer = window.setTimeout(__hd+".goNext()",__step);
            }
        }catch(e){
            __box.innerHTML = "Load Error:"+e.message;
        }
    };
    
    var __movePic = function(){
        
    };
    
    var __refreshContent = function(){
        if(__data.length>__index){
//            __bimg.src = __img.src;
//            __bimg.style.width = __img.offsetWidth+"px";
//            __bimg.style.height = __img.offsetHeight+"px";
//            __bimg.style.left = "-20px";
//            __bimg.style.top = "-20px";
//            window.ani = new JskitAnimation("ani");
//            ani.transform(__bimg,-20,0,0,__bimg.offsetHeight,40);
            __img.src = __data[__index].CoverPath;
            
            for(var i=0;i<__numbar.childNodes.length;i++){
                __numbar.childNodes[i].className = "smartad_num_i";
            }
            __numbar.childNodes[__index].className = "smartad_num_i_c";
        }
    };
    
    var __stop = function(){
        window.clearTimeout(__timer);
        __timer = null;
    };
        
    var __goNext = function(){
        __stop();
        if(__index+1>=__data.length){
            __index = 0;
        }else{
            __index += 1;
        }
        __refreshContent();
        __timer = window.setTimeout(__hd+".goNext()",__step);
    };
    
    
    this.goNext = function(){
        __goNext();
    };
    
    this.change = function(idx){
        __index = idx;
        __refreshContent();
    };
    
    
    
    
    this.setData = function(v){
        __data = v;
    };
    this.setStep = function(v){
        if(!isNaN(parseInt(v))){
            __step = parseInt(v)*1000;
        }
    };
    this.deploy = function(rBoxId,rRequestUrl){
        __box = $("#"+rBoxId);
        __requestUrl = (rRequestUrl!=null && rRequestUrl!="")?rRequestUrl:null;
        if(typeof(rMaxCount)=="number"){
            __maxCount = parseInt(rMaxCount);
        }
         __loadData();       
    };
};